<?php

namespace Home\Model;
require_once COMMON_PATH . 'Service/UserLevel.class.php';

class YaoqingRecordsModel extends CommonModel
{
    public function index($user_id, $to_user, $money, $remark, $type = 1, $order_id='', $data = 0, $role_money = 0, $show_fencheng = 0, $source_uid = 0)
    {
        $User = new UserModel;
        $Self = new YaoqingRecordsModel;

        $money = changefloor($money);

        if ($money > 0) {
            $balance = $User->master(true)->where(['id' => $user_id])->getField('score');
            $current_balance = $data ? ($balance - $money) : ($balance + $money);
            if($current_balance < 0){
                return false;
            }
            if ($data) {
                $User->where(['id' => $user_id])->setDec('score', $money);
            } else {
                $User->where(['id' => $user_id])->setInc('shouyi', $money);
                $User->where(['id' => $user_id])->setInc('score', $money);

                if (stripos($remark, '收益') && stripos($remark, '充值') === false) {
                    // 用户业绩统计
                    (new UserDataModel)->createData($user_id, $money, 'income');
                }
            }
            $Self->add([
                'uid' => $user_id,
                'touid' => $to_user,
                'content' => $remark,
                'money' => $role_money ?: $money,
                'type' => $type,
                'data' => $data,
                'order_id' => $order_id,
                'balance' => $current_balance,
                'time' => time(),
                'show_money' => $show_fencheng ?: $money,
                'source_uid' => $source_uid,
            ]);
            \UserLevel::checkUserLevel($user_id);

            return true;
        }else{
            return false;
        }
    }
}
